<?php

namespace App\Http\Middleware;

use App\Models\PointShare;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;

/**
 * 自定义token认证
 */
class AuthToken {
	public function handle(Request $request, Closure $next) {
		$token = $request->header("token");
		if (empty($token)) {
			return response()->json(['code' => 444, 'msg' => 'token认证失败']);
		} else if (is_array($token)) {
			$token = $token[0];
		}
		$user = User::where("token", $token)->first();
        if (!$user){
            return response()->json(['code' => 444, 'msg' => '登录已失效']);
        }
		$request->user = $user;
		$request->user_id = $user->id;
		return $next($request);
	}
}